# address_manager_page.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class AddressManagerPage:
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://example.com/address-manager'
        self.new_address_button_locator = (By.ID, 'new-address-button')  # 新增地址按钮定位器
        self.edit_address_button_locator = (By.ID, 'edit-address-button')  # 编辑地址按钮定位器
        self.delete_address_button_locator = (By.ID, 'delete-address-button')  # 删除地址按钮定位器
        self.address_list_locator = (By.ID, 'address-list')  # 地址列表定位器
        self.address_form_locator = (By.ID, 'address-form')  # 地址表单定位器
        self.save_address_button_locator = (By.ID, 'save-address-button')  # 保存地址按钮定位器
        self.cancel_address_button_locator = (By.ID, 'cancel-address-button')  # 取消地址按钮定位器
        self.address_input_locator = (By.ID, 'address-input')  # 地址输入框定位器
        self.name_input_locator = (By.ID, 'name-input')  # 收件人输入框定位器
        self.phone_input_locator = (By.ID, 'phone-input')  # 电话输入框定位器
        self.postal_code_input_locator = (By.ID, 'postal-code-input')  # 邮政编码输入框定位器
        self.default_address_checkbox_locator = (By.ID, 'default-address-checkbox')  # 默认地址复选框定位器
        self.address_success_message_locator = (By.ID, 'address-success-message')  # 地址成功消息定位器
        self.address_error_message_locator = (By.ID, 'address-error-message')  # 地址错误消息定位器

    # 导航到地址管理页面
    def navigate(self):
        self.driver.get(self.url)

    # 点击新增地址按钮
    def click_new_address_button(self):
        self.driver.find_element(*self.new_address_button_locator).click()

    # 点击编辑地址按钮
    def click_edit_address_button(self):
        self.driver.find_element(*self.edit_address_button_locator).click()

    # 点击删除地址按钮
    def click_delete_address_button(self):
        self.driver.find_element(*self.delete_address_button_locator).click()

    # 获取地址列表
    def get_address_list(self):
        return self.driver.find_elements(*self.address_list_locator)

    # 填写地址表单
    def fill_address_form(self, name, address, phone, postal_code, is_default=False):
        self.driver.find_element(*self.name_input_locator).send_keys(name)
        self.driver.find_element(*self.address_input_locator).send_keys(address)
        self.driver.find_element(*self.phone_input_locator).send_keys(phone)
        self.driver.find_element(*self.postal_code_input_locator).send_keys(postal_code)
        if is_default:
            self.driver.find_element(*self.default_address_checkbox_locator).click()

    # 保存地址
    def save_address(self):
        self.driver.find_element(*self.save_address_button_locator).click()

    # 取消保存地址
    def cancel_address(self):
        self.driver.find_element(*self.cancel_address_button_locator).click()

    # 获取保存地址后的成功消息
    def get_success_message(self):
        return self.driver.find_element(*self.address_success_message_locator).text

    # 获取保存地址后的错误消息
    def get_error_message(self):
        return self.driver.find_element(*self.address_error_message_locator).text

    # 等待地址保存完成
    def wait_for_save_complete(self):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.address_success_message_locator)
        )